using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LogAnalyzer.Dialogs
{
    public partial class ManageFilters : Form
    {
        private List<FilterBase> _filters;
        private Type _filterType = null;



        public ManageFilters(FilterBase[] filters)
        {
            InitializeComponent();

            _filters = new List<FilterBase>();
            _filterType = filters.GetType().GetElementType();

            switch (_filterType.ToString())
            {
                case "LogAnalyzer.DisplayFilter":
                    this.Text = Globalization.ResourceManager.Current.GetString("MANAGE_DISPLAY_FILTERS");
                    break;
                case "LogAnalyzer.GrabFilter":
                    this.Text = Globalization.ResourceManager.Current.GetString("MANAGE_GRABBER_FILTERS");
                    break;
            }

            if (filters != null)
            {
                FilterBase[] copy = new FilterBase[filters.Length];
                for (int i = 0; i < filters.Length; i++)
                {
                    copy[i] = filters[i].Clone();
                }
                _filters.AddRange(copy);
            }

        }

        public FilterBase[] ModifiedFilters
        {
            get 
            {
                return _filters.ToArray(); 
            }
        }

        private void ShowFilters()
        {
            lstFilters.Items.Clear();
            propFilter.SelectedObject = null;
            int i = 0;
            foreach (FilterBase filter in _filters)
            {
                i++;
                lstFilters.Items.Add(string.Format("{0}: {1}", i, filter));
            }
        }

        private void lstFilters_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstFilters.SelectedIndex == -1)
            {
                propFilter.SelectedObject = null;
            }
            else
            {
                propFilter.SelectedObject = _filters[lstFilters.SelectedIndex];
            }
        }

        private void ManageFilters_Load(object sender, EventArgs e)
        {
            ShowFilters();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            _filters.Add((FilterBase)Activator.CreateInstance(_filterType));
            ShowFilters();
            lstFilters.SelectedIndex = _filters.Count - 1;
            propFilter.Focus();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            int selIdx = lstFilters.SelectedIndex;
            if (selIdx == -1)
                return;

            _filters.RemoveAt(lstFilters.SelectedIndex);
            ShowFilters();

            if (selIdx < lstFilters.Items.Count)
                lstFilters.SelectedIndex = selIdx;
            else if (selIdx == lstFilters.Items.Count)
                lstFilters.SelectedIndex = selIdx - 1;
        }

        private void btnMoveUp_Click(object sender, EventArgs e)
        {
            int selIdx =lstFilters.SelectedIndex;
            if (selIdx > 0 && selIdx < lstFilters.Items.Count)
            {
                FilterBase tmp = _filters[selIdx];
                _filters.RemoveAt(selIdx);
                _filters.Insert(selIdx - 1, tmp);
                ShowFilters();
                lstFilters.SelectedIndex = selIdx - 1;
            }
        }

        private void btnMoveDown_Click(object sender, EventArgs e)
        {
            int selIdx = lstFilters.SelectedIndex;
            if (selIdx > -1 && selIdx < (lstFilters.Items.Count-1))
            {
                FilterBase tmp = _filters[selIdx];
                _filters.RemoveAt(selIdx);
                _filters.Insert(selIdx + 1, tmp);
                ShowFilters();
                lstFilters.SelectedIndex = selIdx + 1;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void propFilter_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            int selIdx = lstFilters.SelectedIndex;
            ShowFilters();
            lstFilters.SelectedIndex = selIdx;
        }

    }

}